<template>
  <div id="userLoginView">
    <h2>用户登录</h2>
    <a-form
      auto-label-width
      style="max-width: 480px; margin: 0 auto"
      :model="form"
      @submit="handleSubmit"
    >
      <a-form-item field="userAccount" label="Username">
        <a-input v-model="form.userAccount" placeholder="账号/邮箱/手机号" />
      </a-form-item>
      <a-form-item field="userPassword" label="Password">
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入登录密码"
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 480px">
          登录
        </a-button>
      </a-form-item>
    </a-form>
  </div>
  <div id="account">
    管理员账号：xixisleep &nbsp; &nbsp; &nbsp;密码：123456 &nbsp;&nbsp;
    <router-link to="/user/register">注册</router-link>
  </div>
</template>

<script setup lang="ts">
import { reactive } from "vue";
import { UserControllerService } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import { useStore } from "vuex";

/**
 * 表单信息
 */
const form = reactive({
  userAccount: "",
  userPassword: "",
});

const router = useRouter();
const store = useStore();

/**
 * 提交表单
 * @param data
 */
const handleSubmit = async () => {
  const res = await UserControllerService.userLoginUsingPost(form);
  //登录成功，跳转到主页
  if (res.code === 0) {
    await store.dispatch("user/getLoginUser");
    router.push({
      path: "/",
      replace: true,
    });
  } else {
    message.error("登录失败，" + res.message);
  }
};
</script>

<style scoped>
#account {
  text-align: center;
}
</style>
